package cn.exrick.manager.controller;

import cn.exrick.common.pojo.DataTablesResult;
import cn.exrick.common.pojo.Result;
import cn.exrick.common.utils.ResultUtil;
import cn.exrick.manager.pojo.TbArticle;
import cn.exrick.manager.pojo.TbUser;
import cn.exrick.manager.service.ArticleService;
import cn.exrick.manager.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/article/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取文章列表")
    public DataTablesResult getArticleList(int draw, int start, int length, TbArticle tbArticle,
                                        @RequestParam("order[0][column]") int orderCol, @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","a.name","a.nav_id", "a.create_time","a.update_time"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "a.update_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }

        DataTablesResult result= articleService.getArticleList(draw,start,length,tbArticle,orderColumn,orderDir);
        return result;
    }

    @RequestMapping(value = "/article/del/{ids}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除文章")
    public Result<Object> delArticle(@PathVariable Long[] ids){
        articleService.deleteArticle(Arrays.asList(ids));
        return new ResultUtil<Object>().setData(null);
    }

    private TbUser getCurrentUser(){
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        TbUser user = userService.getUserByUsername(username);
        return user;
    }

    @RequestMapping(value = "/article/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新文章")
    public Result<Object> updateArticle(TbArticle tbArticle){
        tbArticle.setUpdateBy(getCurrentUser().getId());
        articleService.updateArticle(tbArticle);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/article/add",method = RequestMethod.POST)
    @ApiOperation(value = "新增文章")
    public Result<Object> addArticle(TbArticle tbArticle){
        TbUser user = getCurrentUser();
        tbArticle.setCreateBy(user.getId());
        tbArticle.setUpdateBy(user.getId());
        articleService.addArticle(tbArticle);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/article/{id}",method = RequestMethod.GET)
    public Result<TbArticle> getArticle(@PathVariable Long id){
        TbArticle tbArticle = articleService.getArticle(id);
        return new ResultUtil<TbArticle>().setData(tbArticle);
    }


}
